package com.dehui.traders.ui.trend;

import android.content.Context;
import android.databinding.ObservableField;
import android.databinding.ObservableInt;
import android.support.v4.content.ContextCompat;
import android.widget.TextView;

import com.dehui.traders.R;
import com.dehui.traders.bean.BaseResponse;
import com.dehui.traders.bean.TrendInfo;
import com.dehui.traders.service.CallListener;
import com.dehui.traders.service.NetImpl;
import com.dehui.traders.utils.DateUtil;
import com.dehui.traders.widgets.BaseViewHolder;
import com.dehui.traders.widgets.CommonAdapter;

import java.util.ArrayList;
import java.util.List;

import me.goldze.mvvmhabit.base.BaseViewModel;
import retrofit2.Call;
import retrofit2.Response;


/**
 * Created by hqq on 18.3.30.
 */

public class TrendViewModel extends BaseViewModel{

    private TrendFragment fragment;
    public ObservableField<String> content = new ObservableField<>();

    public ObservableField<String> title = new ObservableField<>();

    public ObservableField<String> time = new ObservableField<>();

    public ObservableInt trendcolor = new ObservableInt();

    private List<TrendInfo.TrendStartegry> trendStrategyList = new ArrayList<>();

    public ObservableField<String> probabilityStr = new ObservableField<>();

    public ObservableField<String> trendtext = new ObservableField<>();


    public CommonAdapter trendListAdapter = new CommonAdapter<TrendInfo.TrendStartegry>(context,trendStrategyList,
                            R.layout.list_item_trend){

        @Override
        public void convert(BaseViewHolder helper, TrendInfo.TrendStartegry item, int position) {
            TextView tvItem = helper.getView(R.id.tv_item);
            tvItem.setText(item.getName());
        }
    };

    public ObservableField<CommonAdapter> trendListAdapterObservable = new ObservableField<>(trendListAdapter);

    public TrendViewModel(Context context, TrendFragment fragment) {
        super(context);
        this.fragment = fragment;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        initTrend();
    }

    public void initTrend() {
        Call<BaseResponse<TrendInfo>> call = NetImpl.queryTrend();
        call.enqueue(new CallListener<BaseResponse<TrendInfo>>() {
            @Override
            public void _onResponse(Response<BaseResponse<TrendInfo>> response) {
                fragment.onComplete();
                BaseResponse<TrendInfo> trendInfoBaseListResponse = response.body();
                if (trendInfoBaseListResponse.status == 200){
                    TrendInfo trendInfo = trendInfoBaseListResponse.result;
                    setTrend(trendInfo);
                }
            }

            @Override
            public void _onFailure() {
                fragment.onComplete();
            }
        });
    }

    private void setTrend(TrendInfo trendInfo) {
        content.set(trendInfo.getREMARKS());
        title.set(trendInfo.getRISK());
        String date = DateUtil.getStringByFormat(trendInfo.getUPDATEDATE(), DateUtil.dateFormatYMDHMS);
        time.set(date);
        trendStrategyList.clear();
        trendStrategyList.addAll(trendInfo.getTrendStrategy());
        trendListAdapter.notifyDataSetChanged();
        String probability = trendInfo.getPROBABILITY();
        probabilityStr.set(probability + "%");
        String trend = trendInfo.getTREND();
        fragment.initRingView(probability,trend);
        trendtext.set(trend);
        if ("上涨".equals(trend)){
            trendcolor.set(ContextCompat.getColor(fragment.getContext(),R.color.red));
        }else{
            trendcolor.set(ContextCompat.getColor(fragment.getContext(),R.color.green));
        }
    }
}
